package com.leonne.harpacristabrasileira.tools;

import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class InternetConnection {
	/**
	 * Verifica o status da conexão com a internet o se o servidor está online
	 * return 
	 * 		<p>0 - Tudo Ok 
	 * 		<p>1 - Problemas com a conexão com a internet 
	 * 		<p>2 - Problemas com a conexão com o servidor
	 */

	public static int checkConnection(Activity parentActivity) {
		int result = 0;
		ConnectivityManager connectivityManager = (ConnectivityManager) parentActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
		result = (activeNetworkInfo != null) ? 0 : 1;
		if (result == 0){
			try {
				URL url = new URL(MyConstants.AUDIOSTREAMINGSERVER);
				HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
			    urlcon.connect();
			    result = (urlcon.getResponseCode() == 200) ? 0 : 2;
			} catch (Exception e) {
				return 2;
			}
		}
		return result;
	}
}
